package uk.ac.cam.juliet.zedcat.socialnetworking.plugin;

import uk.ac.cam.juliet.zedcat.db.Account;
import uk.ac.cam.juliet.zedcat.socialnetworking.AbstractPlugin;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;

public class TwitterPlugin extends AbstractPlugin {

	Twitter t;
	
	public TwitterPlugin(Account acc) {
		super(acc.getAccountID(), acc.getUsername(), acc.getPassword());
		t = new Twitter(username,password);
	}

	public void pushUpdate(String update) throws TwitterException {
		System.out.println("New twitter thread");
		TwitterThread t = new TwitterThread();
		t.setMessage(update);
		t.start();
	}
	
	public class TwitterThread extends Thread {
		String message;
		
		public void setMessage(String message) {
			this.message = message;
		}
		
		public void run() {
			try {
			System.out.println("AccountID: " + accountid + " - Type: Twitter - Pushing update to twitter API.");
			t.updateStatus(message);
			System.out.println("AccountID: " + accountid + " - Type: Twitter - Message sent!");
			} catch (TwitterException e) {
				System.err.println("Could not post twitter update");
			}
		}
	}
}
